package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.huabo.system.mysql.entity.TblManageRightMySql;
import com.huabo.system.mysql.entity.TblOrgRightMySql;
import com.huabo.system.mysql.entity.TblOrgRightnewMySql;
import com.huabo.system.mysql.mapper.TblOrgRightMySqlMapper;
import com.huabo.system.oracle.entity.TblManageRight;
import com.huabo.system.oracle.entity.TblOrgRight;
import com.huabo.system.oracle.entity.TblOrgRightnew;
import com.huabo.system.oracle.mapper.TblOrgRightMapper;
import com.huabo.system.service.TblOrgRightService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class TblOrgRightServiceImpl implements TblOrgRightService {

    @Resource
    private TblOrgRightMapper tblOrgRightMapper;

    @Resource
    private TblOrgRightMySqlMapper tblOrgRightMySqlMapper;

    @Override
    public TblOrgRightnew findByTblOrgRightorgname(String token, String staffId, String rightname) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            List<TblOrgRightnew> list = tblOrgRightMapper.findByTblOrgRight(orgid, rightname);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TblOrgRightnewMySql findByMySqlTblOrgRightorgname(String token, String staffId, String rightname) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            List<TblOrgRightnewMySql> list = tblOrgRightMySqlMapper.findByTblOrgRight(orgid, rightname);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TblOrgRightnew findByTblOrgRightorgid(String token, String staffId, String rightid) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            List<TblOrgRightnew> list = tblOrgRightMapper.findByTblOrgRightId(orgid, rightid);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TblOrgRightnewMySql findByMySqlTblOrgRightorgid(String token, String staffId, String rightid) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            List<TblOrgRightnewMySql> list = tblOrgRightMySqlMapper.findByTblOrgRightId(orgid, rightid);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String findByTblOrgRightreturnname(String token, String staffId, TblManageRight vmr) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            List<Object[]> list = tblOrgRightMapper.OBJlistBySql(orgid, vmr.getRightid().toString());
            if (list != null && list.size() > 0) {
                return list.get(0)[0] != null ? list.get(0)[0].toString() : null;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String findByMySqlTblOrgRightreturnname(String token, String staffId, TblManageRight vmr) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            List<Object[]> list = tblOrgRightMySqlMapper.OBJlistBySql(orgid, vmr.getRightid().toString());
            if (list != null && list.size() > 0) {
                return list.get(0)[0] != null ? list.get(0)[0].toString() : null;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateTblOrgRight(String token, String staffId, TblOrgRightnew orgright) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            orgright.setOrgid(Integer.parseInt(orgid.toString()));
            tblOrgRightMapper.updateByOrgright(orgright);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateMySqlTblOrgRight(String token, String staffId, TblOrgRightnewMySql orgright) {
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            orgright.setOrgid(Integer.parseInt(orgid.toString()));
            tblOrgRightMySqlMapper.updateByOrgright(orgright);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void saveTblOrgRight(TblOrgRightnew orgright) {
        this.tblOrgRightMapper.insertSelective(orgright);
    }

    @Override
    public void saveMySqlTblOrgRight(TblOrgRightnewMySql orgright) {
        this.tblOrgRightMySqlMapper.insertSelective(orgright);
    }

    @Override
    public void updateTblOrgRights(TblOrgRightnew orgright) {
        this.tblOrgRightMapper.updateTblOrgRights(orgright);
    }

    @Override
    public void updateMySqlTblOrgRights(TblOrgRightnewMySql orgright) {
        this.tblOrgRightMySqlMapper.updateTblOrgRights(orgright);
    }

    @Override
    public void add(TblOrgRight right) {
        this.tblOrgRightMapper.insertOrgRight(right);
    }

    @Override
    public void addMySql(TblOrgRightMySql right) {
        this.tblOrgRightMySqlMapper.insertOrgRight(right);
    }
}
